package com.ca.ridersclubapplication.view.input;

import android.app.Dialog;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.ca.ridersclubapplication.R;
import com.ca.ridersclubapplication.UpdateActivity;
import com.ca.ridersclubapplication.request.dto.AccountUpdateDto;

public class GenderSelector extends ConstraintLayout {

    public UpdateActivity activity;

    public String gender;
    public Dialog dialog;
    public AccountUpdateDto dto;

    public TextView maleView;
    public TextView femaleView;
    public TextView ukView;

    public GenderSelector(@NonNull Context context, UpdateActivity activity, AccountUpdateDto dto, String gender) {
        super(context);
        initView(context);

        this.activity = activity;
        this.gender = gender;

        if ("男".equals(gender)) {
            maleView.setTextColor(getResources().getColor(R.color.primary));
            femaleView.setTextColor(getResources().getColor(R.color.primary_text));
            ukView.setTextColor(getResources().getColor(R.color.primary_text));
        }
        else if ("女".equals(gender)) {
            maleView.setTextColor(getResources().getColor(R.color.primary_text));
            femaleView.setTextColor(getResources().getColor(R.color.primary));
            ukView.setTextColor(getResources().getColor(R.color.primary_text));
        }
        else {
            maleView.setTextColor(getResources().getColor(R.color.primary_text));
            femaleView.setTextColor(getResources().getColor(R.color.primary_text));
            ukView.setTextColor(getResources().getColor(R.color.primary));
        }

        //  点击事件
        maleView.setOnClickListener(v -> {
            maleView.setTextColor(getResources().getColor(R.color.primary));
            femaleView.setTextColor(getResources().getColor(R.color.primary_text));
            ukView.setTextColor(getResources().getColor(R.color.primary_text));
            dto.setGender("男");
            activity.update(dto);
            dialog.dismiss();
        });
        femaleView.setOnClickListener(v -> {
            maleView.setTextColor(getResources().getColor(R.color.primary_text));
            femaleView.setTextColor(getResources().getColor(R.color.primary));
            ukView.setTextColor(getResources().getColor(R.color.primary_text));
            dto.setGender("女");
            activity.update(dto);
            dialog.dismiss();
        });
        ukView.setOnClickListener(v -> {
            maleView.setTextColor(getResources().getColor(R.color.primary_text));
            femaleView.setTextColor(getResources().getColor(R.color.primary_text));
            ukView.setTextColor(getResources().getColor(R.color.primary));
            dto.setGender("保密");
            activity.update(dto);
            dialog.dismiss();
        });
    }

    public GenderSelector(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public GenderSelector(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.gender_selector_layout, this);
        maleView = view.findViewById(R.id.tv_male);
        femaleView = view.findViewById(R.id.tv_female);
        ukView = view.findViewById(R.id.tv_uk);
    }
}
